package Order_Function;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import Goods_Utils.Good;
import JDBC.JDpoor;

public class Goods_Function {
	Connection con = new JDpoor().CreateCon();;
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public ArrayList<Good> getAllGoods() {
		ArrayList<Good> goods = new ArrayList<Good>();
		
		String condition = "select * from goods_info";
		
		try {
			pre = con.prepareStatement(condition);
			rs = pre.executeQuery();
			
			while(rs.next()){
				Good good = new Good();
				good.setName(rs.getString("name"));
				good.setPrice(rs.getString("price"));
				good.setType(rs.getString("type"));
				good.setStock(rs.getString("stock"));
				good.setUrl(rs.getString("url"));
				goods.add(good);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
		return goods;
		
		
		
	}

	public ArrayList<Good> getTypeGoods(String type) {
		// TODO Auto-generated method stub
		ArrayList<Good> goods = new ArrayList<Good>();
		
		String condition = "select * from goods_info where type=?";
		
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, type);
			rs = pre.executeQuery();
			
			while(rs.next()){
				Good good = new Good();
				good.setName(rs.getString("name"));
				good.setPrice(rs.getString("price"));
				good.setType(rs.getString("type"));
				good.setStock(rs.getString("stock"));
				good.setUrl(rs.getString("url"));
				goods.add(good);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
		return goods;
	}
	
	
}
